package com.data.center.util.ExcelUtils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;

import java.util.*;

public class CustomStringToMapConverter implements Converter<Map> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return List.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * @description:这里读的时候会调用，通过英文逗号将字符串分割为一个个的字符串，然后放入List中
     *
     * @param context
     * @return
     */
    @Override
    public Map convertToJavaData(ReadConverterContext<?> context) {
        Map<String, String> additionalInformation = new HashMap<>();
        if (context.getReadCellData().getStringValue() != null) {
            List<String> listValue = new ArrayList<>();
            listValue.addAll(Arrays.asList(context.getReadCellData().getStringValue().split(",")));
            for (String mapItem: listValue) {
                List<String> keyValue = Arrays.asList(mapItem.split(":"));
                additionalInformation.put(keyValue.get(0), keyValue.get(1));
            }
        }
        return additionalInformation;
    }

//    /**
//     * 这里是写的时候会调用 不用管
//     *
//     * @return
//     */
//    @Override
//    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
//        return new WriteCellData<>(context.getValue());
//    }

}